package com.soladhoc.webapps.server.guice;

import com.google.inject.Inject;
import com.googlecode.objectify.ObjectifyService;
import com.gwtplatform.dispatch.server.ExecutionContext;
import com.gwtplatform.dispatch.server.actionhandler.ActionHandler;
import com.gwtplatform.dispatch.shared.ActionException;
import com.soladhoc.webapps.client.domain.Announce;

public class SaveAnnounceActionHandler implements
		ActionHandler<SaveAnnounceAction, SaveAnnounceActionResult> {

	@Inject
	public SaveAnnounceActionHandler() {
	}

	@Override
	public SaveAnnounceActionResult execute(SaveAnnounceAction action,
			ExecutionContext context) throws ActionException {
		final Announce announceToSave = action.getAnnounceToSave();
		ObjectifyService.begin().put(announceToSave);
		return new SaveAnnounceActionResult(announceToSave);
	}

	@Override
	public void undo(SaveAnnounceAction action,
			SaveAnnounceActionResult result, ExecutionContext context)
			throws ActionException {
		ObjectifyService.begin().delete(result.getAnnounce());
	}

	@Override
	public Class<SaveAnnounceAction> getActionType() {
		return SaveAnnounceAction.class;
	}
}
